package com.pn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pn.entity.Brand;
import com.pn.entity.Place;
import com.pn.mapper.BrandMapper;
import com.pn.mapper.PlaceMapper;
import com.pn.service.BrandService;
import com.pn.service.PlaceService;
import com.pn.utils.PageBean;
import com.pn.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@CacheConfig(cacheNames = "com.pn.service.impl.PlaceServiceImpl")
@Service
public class PlaceServiceImpl implements PlaceService {

    @Autowired
    private PlaceMapper placeMapper;

    //显示产地信息
    @Override
    public PageBean placePageList(Place place) {
        PageHelper.startPage(place.getPageNum(),place.getPageSize());
        Page<Place> page = placeMapper.placePageList(place);
        return new PageBean(page.getTotal(),page.getResult());
    }

    //修改产地
    @CacheEvict(key = "'all:place'")
    @Override
    public Result placeUpdate(Place place) {

        int i = placeMapper.placeUpdate(place);
        if (i > 0 ){
            return Result.ok("修改品牌成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"修改品牌失败");
    }

    //添加产地
    @CacheEvict(key = "'all:place'")
    @Override
    public Result placeAdd(Place place) {

        int i = placeMapper.placeInsert(place);
        if (i > 0 ){
            return Result.ok("添加品牌成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"添加品牌失败");
    }

    //删除产地
    @CacheEvict(key = "'all:place'")
    @Override
    public Result placeDelete(Integer placeId) {

        int i = placeMapper.placeDelete(placeId);
        if (i > 0 ){
            return Result.ok("删除品牌成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"删除品牌失败");
    }

    //显示产地
    @Cacheable(key = "'all:place'")
    @Override
    public List<Place> placeList() {
        List<Place> placeList =  placeMapper.placeList();
        return placeList;
    }
}
